package com.jf.hotelreservation.demos.service.impl;

import com.alibaba.fastjson2.JSON;
import com.jf.hotelreservation.demos.bean.Overview;
import com.jf.hotelreservation.demos.mapper.OverviewMapper;
import com.jf.hotelreservation.demos.service.OverviewService;
import com.jf.hotelreservation.demos.utils.DataUtils;
import com.jf.hotelreservation.demos.utils.PageBean;
import com.jf.hotelreservation.demos.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OverviewServiceImpl implements OverviewService {

    @Autowired
    private OverviewMapper overviewMapper;
    @Override
    public String addOverview(Overview overview) {
        if (overviewMapper.addOverview(overview) != 0){
            return JSON.toJSONString(R.success("添加成功"));
        }else {
            return JSON.toJSONString(R.error("添加失败"));
        }

    }

    @Override
    public String queryAll(Integer currentPage, Integer pageSize) {
        List<Overview> overviewList = overviewMapper.queryAll(DataUtils.getStartRecord(currentPage,pageSize),pageSize);
        long count = overviewMapper.count();
        if (overviewList.isEmpty()){
            return JSON.toJSONString(R.error("当前无数据"));
        }else {
            PageBean pageBean = new PageBean();
            pageBean.setTotal(count);
            pageBean.setRows(overviewList);
            return JSON.toJSONString(R.success("查询成功",pageBean));
        }
    }

    @Override
    public String queryByHotel() {
        List<Overview> overviewList = overviewMapper.queryByHotel();
        if (overviewList.isEmpty()){
            return JSON.toJSONString(R.error("查询失败"));
        }else {
            return JSON.toJSONString(R.success(overviewList));
        }
    }
}
